Add a dedicated validator for interpolation.

We need this because not all agents perform interpolation in their
validate_options methods. Now that we have validate_interpolation,
interpolation failure in other validators are simply ignored.

Akinori MUSHA 10 years ago
parent
commit
c706bebcc1
2 changed files with 12 additions and 2 deletions
  1. 11 1
      app/concerns/liquid_interpolatable.rb
  2. 1 1
      spec/concerns/liquid_interpolatable_spec.rb

+ 11 - 1
app/concerns/liquid_interpolatable.rb

@@ -1,10 +1,20 @@
1 1
 module LiquidInterpolatable
2 2
   extend ActiveSupport::Concern
3 3
 
4
+  included do
5
+    validate :validate_interpolation
6
+  end
7
+
4 8
   def valid?(context = nil)
5 9
     super
10
+  rescue Liquid::Error
11
+    errors.empty?
12
+  end
13
+
14
+  def validate_interpolation
15
+    interpolated
6 16
   rescue Liquid::Error => e
7
-    errors.add(:base, e.message)
17
+    errors.add(:options, "has an error with Liquid templating: #{e.message}")
8 18
     false
9 19
   end
10 20
 

+ 1 - 1
spec/concerns/liquid_interpolatable_spec.rb

@@ -29,7 +29,7 @@ describe LiquidInterpolatable::Filters do
29 29
     it "should finish without raising an exception" do
30 30
       agent = Agents::InterpolatableAgent.new(name: "test", options: { 'foo' => '{{bar}' })
31 31
       agent.valid?.should == false
32
-      agent.errors[:base].first.should =~ /not properly terminated/
32
+      agent.errors[:options].first.should =~ /not properly terminated/
33 33
     end
34 34
   end
35 35
 end